﻿
#region USING

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#endregion

namespace Learning.ConwaysGameOfLife
{
    public class GameOfLife
    {
        #region Fields (1) 

        private SquareGrid grid;

        #endregion Fields 

        #region Constructors (1) 

        public GameOfLife(int size, IList<Cordinate> initialAliveCells)
        {
            this.grid = new SquareGrid(size, initialAliveCells);

            this.SetIntialAliveCellsInTheGrid(initialAliveCells);
        }

        #endregion Constructors 

        #region Properties (1) 

        public IEnumerable<Cell> AliveCells
        {
            get
            {
                return this.grid.AliveCells;
            }
        }

        #endregion Properties 

        #region Methods (6) 

        // Public Methods (3) 

        public void AdvanceToNextGeneration()
        {
            this.ProcessGenerationShift();
        }

        public CellState GetCellState(Cordinate cordinate)
        {
            return this.grid.GetNextCellState(cordinate);
        }

        public void SetCellState(Cordinate cordinate, CellState newState)
        {
            this.grid.SetNextCellState(cordinate, newState);
        }
        // Private Methods (3) 

        private CellState DetermineCellState(Cordinate cordinate)
        {
            Cell currentCell = this.grid.FindCell(cordinate);
            IList<Cell> neighbors = this.grid.FindNeighbors(cordinate);
            GameOfLifeRules rules = new GameOfLifeRules(currentCell, neighbors);
            return rules.DetermineNextCellState();
        }

        private void ProcessGenerationShift()
        {
            foreach (var cordinate in this.grid.GetCordinates())
            {
                CellState cellNextState = this.DetermineCellState(cordinate);
            }
        }

        private void SetIntialAliveCellsInTheGrid(IList<Cordinate> liveCells)
        {
            this.grid.ChangeCellsStateToAlive(liveCells);
        }

        #endregion Methods 
    }
}